using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace JA.Web.Apresentacao
{
    public class Util
    {

        /// <summary>
        /// Define o texto de query string que será usado para passar o nome do relatório
        /// </summary>
        public const string CONST_QUERY_STRING_REPORT_NAME = "reportName";

        public static void WindowOpen(Page pagina, string idImovel)
        {

            string paginaParaAbrir = "window.open('FaleConosco.aspx?idImovel=" + idImovel + "', '_blank','menubar=no,width=520,height=570,toolbar=no,resizable=no')";

            ScriptManager.RegisterStartupScript(pagina, pagina.GetType(), "window" + "_" + pagina.ClientID, paginaParaAbrir, true);

        }

        public static void WindowOpenVenda(Page pagina)
        {

            string paginaParaAbrir = "window.open('FaleConoscoVenda.aspx', '_blank','menubar=no,width=520,height=550,toolbar=no,resizable=no')";

            ScriptManager.RegisterStartupScript(pagina, pagina.GetType(), "window" + "_" + pagina.ClientID, paginaParaAbrir, true);

        }


        public static void ExibeAlert(Page page, string msg)
        {
            //AjaxScriptManager.RegisterStartupScript(page, page.GetType(), "alert" + "_" + page.ClientID, "alert('" + msg + "')", true);

            ScriptManager.RegisterStartupScript(page, page.GetType(), "alert" + "_" + page.ClientID, "alert('" + msg + "');", true);
        }

        //Metodo para limpar formulario
        public static void LimparCampos(Control pnCad)
        {

            foreach (Control ctrl in pnCad.Controls)
            {
                if (ctrl is TextBox)
                {
                    TextBox controleLimpar = (TextBox)ctrl;
                    controleLimpar.Text = string.Empty;
                }
                else if (ctrl is DropDownList)
                {
                    DropDownList controleLimpar = (DropDownList)ctrl;
                    controleLimpar.SelectedIndex = 0;
                }

            }
        }
        public static void HabilitarCampos(bool habilitar, Control controle)
        {

            foreach (Control ctrl in controle.Controls)
            {
                if (ctrl.HasControls())
                {
                    HabilitarCampos(habilitar, ctrl);
                }
                if (ctrl is TextBox || ctrl is CheckBox || ctrl is DropDownList || ctrl is RadioButtonList)
                {
                    ((WebControl)ctrl).Enabled = habilitar;
                }
            }
        }

        public static void CarregarComboUF(DropDownList comboUF)
        {
            comboUF.Items.Insert(0, new ListItem("São Paulo", "SP"));
            comboUF.Items.Insert(1, new ListItem("Acre", "AC"));
            comboUF.Items.Insert(2, new ListItem("Alagoas", "AL"));
            comboUF.Items.Insert(3, new ListItem("Amapá", "AP"));
            comboUF.Items.Insert(4, new ListItem("Amazonas", "AM"));
            comboUF.Items.Insert(5, new ListItem("Bahia", "BA"));
            comboUF.Items.Insert(6, new ListItem("Ceará", "CE"));
            comboUF.Items.Insert(7, new ListItem("Distrito Federal", "DF"));
            comboUF.Items.Insert(8, new ListItem("Espírito Santo", "ES"));
            comboUF.Items.Insert(9, new ListItem("Goiás", "GO"));
            comboUF.Items.Insert(10, new ListItem("Maranhão", "MA"));
            comboUF.Items.Insert(11, new ListItem("Mato Grosso", "MT"));
            comboUF.Items.Insert(12, new ListItem("Mato Grosso do Sul", "MS"));
            comboUF.Items.Insert(13, new ListItem("Minas Gerais", "MG"));
            comboUF.Items.Insert(14, new ListItem("Pará", "PA"));
            comboUF.Items.Insert(15, new ListItem("Paraíba", "PB"));
            comboUF.Items.Insert(16, new ListItem("Paraná", "PR"));
            comboUF.Items.Insert(17, new ListItem("Pernambuco", "PE"));
            comboUF.Items.Insert(18, new ListItem("Piauí", "PI"));
            comboUF.Items.Insert(19, new ListItem("Rio de Janeiro", "RJ"));
            comboUF.Items.Insert(20, new ListItem("Rio Grande do Norte", "RN"));
            comboUF.Items.Insert(21, new ListItem("Rio Grande do Sul", "RS"));
            comboUF.Items.Insert(22, new ListItem("Rondônia", "RO"));
            comboUF.Items.Insert(23, new ListItem("Rorâima", "RR"));
            comboUF.Items.Insert(24, new ListItem("Santa Catarina", "SC"));
            comboUF.Items.Insert(25, new ListItem("Sergipe", "SE"));
            comboUF.Items.Insert(26, new ListItem("Tocantins", "TO"));
            comboUF.SelectedValue = "SP";

        }

        public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
            return returnImage;
        }
        public static byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }

        public static System.Drawing.Image FixedSize(System.Drawing.Image imgPhoto, int Width, int Height)
        {
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)Width / (float)sourceWidth);
            nPercentH = ((float)Height / (float)sourceHeight);
            if (nPercentH < nPercentW)
            {
                nPercent = nPercentH;
                destX = System.Convert.ToInt16((Width -
                              (sourceWidth * nPercent)) / 2);
            }
            else
            {
                nPercent = nPercentW;
                destY = System.Convert.ToInt16((Height -
                              (sourceHeight * nPercent)) / 2);
            }

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(Width, Height,
                              PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                             imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.Clear(Color.White);
            grPhoto.InterpolationMode =
                    InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }
        public static System.Drawing.Image MaxSize(System.Drawing.Image imgPhoto, int maxWidth, int maxHeight)
        {
            int newWidth = imgPhoto.Width;
            int newHeight = imgPhoto.Height;

            float factor = 0;
            if (newWidth > maxWidth)
            {
                factor = (float)maxWidth / (float)newWidth;
                newWidth = maxWidth;
                newHeight = (int)(newHeight * factor);
            }
            if (newHeight > maxHeight)
            {
                factor = (float)maxHeight / (float)newHeight;
                newWidth = (int)(newWidth * factor);
                newHeight = maxHeight;
            }


            Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
                              PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                             imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.Clear(Color.White);
            grPhoto.InterpolationMode =
                    InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                new Rectangle(0, 0, newWidth, newHeight),
                new Rectangle(0, 0, imgPhoto.Width, imgPhoto.Height),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }
        public static System.Drawing.Image ScaleByPercent(System.Drawing.Image imgPhoto, int Percent)
        {
            float nPercent = ((float)Percent / 100);

            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;

            int destX = 0;
            int destY = 0;
            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }
    
    }
}
